﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UserControlLibrary
{
    /// <summary>
    /// Treeview ignore double click at checkbox
    /// \nThis treeview used to prevent possible bug with TreeNode check box (checked state inconsistent)
    /// \n http://stackoverflow.com/questions/14647216/c-sharp-treeview-ignore-double-click-only-at-checkbox
    /// \n http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/9d717ce0-ec6b-4758-a357-6bb55591f956/
    /// </summary>
    public class MyTreeView : TreeView
    {
        // Option 1: Completely disable the double click event.
        /*
        protected override void WndProc(ref Message m)
        {
            // Suppress WM_LBUTTONDBLCLK
            if (m.Msg == 0x203) { m.Result = IntPtr.Zero; }
            else base.WndProc(ref m);
        }
        */

        // Option 2: Treat a double click event as two single click events
        /// <summary>
        /// Overide Windows messages processes 
        /// </summary>
        /// <param name="m">Operating system messages</param>
        protected override void WndProc(ref Message m)
        {
            // WM_LBUTTONDBLCLK
            if (m.Msg == 0x0203)
            {
                // WM_LBUTTONDOWN 
                m.Msg = 0x0201;
            }
            base.WndProc(ref m);
        }
    }
}
